﻿var addproductGrid = addproductGrid || {};

addproductGrid = function () {
    var gridInEditMode = false;
    var addproductDropDownChanged = false;
    var addproductDataSource = new kendo.data.DataSource({
        transport: {

            read: {
                url: "/AddProduct/GetAddProducts",
                type: "get",
                cache: false,
                dataType: "json"
            },
            create: {
                type: "POST",
                url: "/AddProduct/CreateAddProducts",
                type: 'POST',
                dataType: "json",
                contentType: "application/JSON"
            },
     

            update: {
                url: "/AddProduct/UpdateAddProduct",
                type: 'POST',
                dataType: "json",
                contentType: "application/JSON"
            },
            destroy: {
                url: "/AddProduct/DeleteAddProduct",
                type: "POST",
                dataType: "json",
                contentType: "application/JSON"
            },
            parameterMap: function (options) {
                return kendo.stringify(options); // kendo.stringify serializes to JSON string
            }

        },

        pageSize: 10,
        schema: {
            model: {
                id: "id",
                fields: {
                    id: {
                        editable: false, nullable: true,
                        type: "int"
                    },
                    statusName: {
                        type: "string",
                        
                        validation: { required: true }
                    },
                    statusId: {
                        type: "int",
                        defaultValue: 0
                    },
                    AuthorEmployeeId:
                        {
                            type: "int",
                            defaultValue: 0
                           

                        },

                    product: {

                        type: "string",
                        validation: { required: true }
                    },


                    description: {
                        
                        type: "string"
                    },
                    empName: {
                      
                        type: "string"
                    },
                    employeeId: {
                        type: "int",
                      
                    },
                    vendorcompanyId: {                     
                        type: "int",
                        defaultValue: 0
                    },

                    compName:
                        {
                           
                            type: "int"
                        },

                    companyname:
                 {
                     type: "string"

                 },
                    notes: {
                        type: "string"
                       
                    },

                    error: function (a) {
                        $('#addproductGrid').data("kendoGrid").cancelChanges();
                    },
                }
            }
        }

    });


    var status = new kendo.data.DataSource({
        transport: {
            read: {

                url: "/ScratchPad/GetStatus/",
                type: "get",
                contentType: "application/json",
                dataType: "json"
            }
        }
    });

    var authoEmployee = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/AddProduct/GetAuthorEmployee/",
                type: "get",
                contentType: "application/json",
                dataType: "json"
            }
        }
    });
    var VendorCompany = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/Vendor/GetVendorCompanies",
                type: "get",
                contentType: "application/json",
                cache: false,
                dataType: "json"
            }
        }
    });

    return {
        addproductDataSource: addproductDataSource,
        status: status,
        authoEmployee: authoEmployee,
        VendorCompany: VendorCompany,
        addproductDropDownChanged: addproductDropDownChanged,
        gridInEditMode: gridInEditMode


    }

}();

$(function () {

    $(window).on("beforeunload", function () {
        if (addproductGrid.gridInEditMode) {
            return "Are you sure you want to discard all your changes."
        }

    });


    $("#addproductGrid").kendoGrid({
        dataSource: addproductGrid.addproductDataSource,
        scrollable: true,
        height: "250px",
        toolbar: [{ name: "create", text: "Add Product" }, "excel"], //, "save", "cancel"],
        excel: {
            fileName: "ProductExport.xlsx"
        },
        pageable: true,
        height: 500,
        columns: [
 
      { field: "product", title: "Product*", filterable: true, width: "8em" },
      { field: "description", title: "Description", filterable: true, width: "8em" },
      { field: "empName", title: "AuthorEmployee*", filterable: true, width: "8em", editor: authorEmployeeDropDownEditor },
         { field: "companyname", title: "VendorCompany*", filterable: true, width: "8em", editor: vendorCompanyDropDownEditor },

      { field: "notes", title: "Notes", filterable: true, width: "8em" },
      { field: "statusName", title: "Status*", filterable: true, editor: statusDropDownEditor, width: "8em" },

      {
          title: "&nbsp;",
          width: "12em",
          //  command: ["edit", "destroy"]


          command: [

              "edit",
              {
                  name: "Delete",


                  click: function (e) {

                      var tr = $(e.target).closest("tr"); //get the row for deletion
                      var dataItem = this.dataItem(tr); //get the row data so it can be referred later
                      ////see what is coming in dataItem
                      //if (dataItem.QuestionAnswers.length > 0) {
                      //    $("#delValidation").addClass('in')
                      //}
                      //   else {

                      deleteTopicConfirmation(e, this);
                      //  }
                      return false;
                  }
              }
          ]
      }
        ],
        filterable: {
            extra: false,
            operators: {
                string: {
                    eq: "Is equal to",
                    neq: "Is not equal to",
                    contains: "Contains"
                }
            }
        },

        editable: "popup",
        sortable: {
            mode: "single",
            allowUnsort: false
        },
        selectable: true,
        edit: function () {
            addproductGrid.gridInEditMode = true;
        },
        cancel: function () {
            addproductGrid.gridInEditMode = false;
        },
        excelExport: function (e) {
            for (var i = 0; i < e.workbook.sheets[0].columns.length; i++) {
                e.workbook.sheets[0].columns[i].autoWidth = true;
            }
        }
    });

    addproductGrid.addproductDataSource.bind("requestEnd", showSuccessMessage);


    function showSuccessMessage(e) {
        if (e.type === "update" || e.type === "create") {
            addproductGrid.gridInEditMode = false;
            $("#saveConfirm").addClass('in');
            e.sender.read();
        }
    }


    $(".close").click(function () {
        $(this).parent().removeClass('in');
    });


    function deleteTopicConfirmation(e, widget) {
        var grid = $('#addproductGrid').data("kendoGrid");
        deleteConfirmation(e, widget, grid);
    }

    function deleteConfirmation(e, widget, grid) {

        var currentRow = $(e.target).closest("tr"); //get the row for deletion

        var currentRowData = widget.dataItem(currentRow); //get the row data so it can be  referred later

        var kendoWindow = $("<div />").kendoWindow({

            title: "Confirmation",
            resizable: true,
            modal: true,
            width: "400px"
        });

        kendoWindow.data("kendoWindow")

            .content($("#delete-confirmation").html())

            .center().open();

        kendoWindow

            .find(".delete-confirm,.delete-cancel")
            .click(function () {
                if ($(this).hasClass("delete-confirm")) {

                    grid.dataSource.remove(currentRowData);

                    grid.dataSource.sync();

                }

                $(this).closest("[data-role=window]").kendoWindow("close");

            });

    }

    function authorEmployeeDropDownEditor(container, options) {

        $('<input data-text-field="empName" data-value-field="originalAuthorEmployeeId" data-bind="value:AuthorEmployeeId"/>').appendTo(container).kendoDropDownList({
            autoBind: true,          
            dataSource: addproductGrid.authoEmployee

        });
    }
    function statusDropDownEditor(container, options) {
      
        $('<input id="StatusValue" data-text-field="statusName" data-value-field="status"  data-bind="value:statusId"/>').appendTo(container).kendoDropDownList({
            autoBind: true,           
            dataSource: addproductGrid.status

        });
    }

    function vendorCompanyDropDownEditor(container, options) {
    
        $('<input id="StatusValue" data-text-field="companyname" data-value-field="companyId" data-bind="value:vendorcompanyId"/>').appendTo(container).kendoDropDownList({
            autoBind: true,
            
            dataSource: addproductGrid.VendorCompany

        });
    }



});


